#!/usr/bin/env bash
#用法 ./doc/package.md

CORDOVA_PATH=$( cd "$( dirname "$0" )" && pwd -P)

#参数获取
INDEX=0
for param in $*
  do
    # echo $INDEX "--" $param
    array[$INDEX]=$param
    INDEX=$[ $INDEX+1 ]
  done

PROJECT_PATH=${array[0]}
OBJECT_PATH=${array[1]}
p12=${array[2]}
p12Password=${array[3]}
mobileprovision=${array[4]}
schemeTarget=${array[5]}
keychain=${array[6]}


# # 检测是否存在Podfile
# isHavePodfile=$( ls "$PROJECT_PATH" | grep Podfile  )
# if [ -n "$isHavePodfile" ]; then
#
#   # 检测是否安装了pod
#   isHavePod=$( which pod )
#   if [ -z "$isHavePod" ]; then
#     echo "pod不存在，请先安装pod"
#     exit 1
#   fi
#
#   isHavePodfileLock=$( ls "$PROJECT_PATH" | grep Podfile.lock  )
#   cd "$PROJECT_PATH"
#   if [ -n "$isHavePodfileLock" ];then
#     echo "存在Podfile.lock文件"
#     pod update
#   else
#     echo "不存在Podfile.lock文件"
#     #开始安装依赖项
#     pod install
#   fi
#
# fi




#安装p12
echo "-------------------------------------安装p12证书-------------------------------------------"
if [ -z "$p12" ]; then
    echo "请输入p12的地址"
    exit 1
  else
    if [ -z "$p12Password" ]; then
      echo "请输入p12的密码"
      exit 1
    else
      echo $p12Password
      #创建钥匙链
      # security create-keychain -p foreveross foreveross.keychain
      #解锁，否则回弹框等待输入密码
      #security unlock-keychain -u login.keychain
      security unlock-keychain -p '$keychain' /Users/BSL/Library/Keychains/login.keychain
      #导入证书到keychain中
      security import $p12 -k login.keychain -P "$p12Password" -T /usr/bin/codesign
    fi
fi

#安装mobileprovision证书
echo "-------------------------------------安装mobileprovision证书-------------------------------------------"

if [ -n "$mobileprovision" ]; then
    echo "mobileprovision: $mobileprovision"
    uuid=`grep UUID -A1 -a ${mobileprovision} | grep -io "[-A-Z0-9]\{36\}"`
    output=~/Library/MobileDevice/Provisioning\ Profiles/$uuid.mobileprovision
    dir=~/Library/MobileDevice/Provisioning\ Profiles

    if [ ! -d "$dir" ]; then
      echo "新建文件夹：$dir"
      cd ~/Library/MobileDevice/
      mkdir -p "$dir"
    fi

    cp  "$mobileprovision" "$output"
    echo "Found UUID $uuid"
  else
    echo "请输入mobileprovision的地址"
    exit 1
fi
# 查看uuid是否获取
if [ -z "$uuid" ]; then
  echo "没有获取到uuid,请查看输入的mobileprovision证书地址是否正确"
  exit 1
fi

if [ -z "$schemeTarget" ]; then
  echo "如果你的项目为workspace，请输入scheme;如果你的项目为projece，请输入target"
  exit 1
fi


"$CORDOVA_PATH/clean" $PROJECT_PATH $schemeTarget

#编译
mkdir "$OBJECT_PATH/archive"
"$CORDOVA_PATH/build" $PROJECT_PATH $schemeTarget --device $uuid|| exit $?

echo "-----------------------------------开始打包-----------------------------------------"
# 判断是否存在workspace 并获取相应的项目信息
if [ ! $( ls "$PROJECT_PATH" | grep .xcworkspace  ) ]; then
  #项目名称
  XCODEPROJ=$( ls "$PROJECT_PATH" | grep .xcodeproj  )
  PROJECT_NAME=$(basename "$XCODEPROJ" .xcodeproj)
  xcodebuild archive -project "$PROJECT_PATH/$PROJECT_NAME.xcodeproj" -scheme "$schemeTarget" -archivePath "$PROJECT_PATH/build/device/$PROJECT_NAME.xcarchive"
  xcodebuild -exportArchive -archivePath "$PROJECT_PATH/build/device/$PROJECT_NAME.xcarchive" -exportPath "$OBJECT_PATH/archive/app.ipa" -exportFormat ipa PROVISIONING_PROFILE=$uuid
else
  #获取项目空间相应信息
  XCWORKSPACE=$( ls "$PROJECT_PATH" | grep .xcworkspace  )
  XCWORKSPACE_NAME=$(basename "$XCWORKSPACE" .xcworkspace)
  xcodebuild archive -workspace "$PROJECT_PATH/$XCWORKSPACE_NAME.xcworkspace" -scheme "$schemeTarget" -archivePath "$PROJECT_PATH/build/device/$XCWORKSPACE_NAME.xcarchive"
  xcodebuild -exportArchive -archivePath "$PROJECT_PATH/build/device/$XCWORKSPACE_NAME.xcarchive" -exportPath "$OBJECT_PATH/archive/app.ipa" -exportFormat ipa PROVISIONING_PROFILE=$uuid
fi

# # 判断是否存在workspace 并获取相应的项目信息
# if [ ! $( ls "$PROJECT_PATH" | grep .xcworkspace  ) ]; then
#   #项目名称
#   XCODEPROJ=$( ls "$PROJECT_PATH" | grep .xcodeproj  )
#   PROJECT_NAME=$(basename "$XCODEPROJ" .xcodeproj)
#   exec xcodebuild -xcconfig "$CORDOVA_PATH/build.xcconfig" -project "$PROJECT_NAME.xcodeproj" ARCHS="armv7 armv7s arm64" -target "$schemeTarget" -configuration $CONFIGURATION -sdk iphoneos build VALID_ARCHS="armv7 armv7s arm64" CONFIGURATION_BUILD_DIR="$PROJECT_PATH/build/device" PROVISIONING_PROFILE=$uuid
# else
#   #获取项目空间相应信息
#   XCWORKSPACE=$( ls "$PROJECT_PATH" | grep .xcworkspace  )
#   XCWORKSPACE_NAME=$(basename "$XCWORKSPACE" .xcworkspace)
#   exec xcodebuild -xcconfig "$CORDOVA_PATH/build.xcconfig" -workspace "$XCWORKSPACE_NAME.xcworkspace" ARCHS="armv7 armv7s arm64" -scheme "$schemeTarget" -configuration $CONFIGURATION -sdk iphoneos build VALID_ARCHS="armv7 armv7s arm64" CONFIGURATION_BUILD_DIR="$PROJECT_PATH/build/device" PROVISIONING_PROFILE=$uuid
# fi

